package com.soft.idc.module.mainpage;


import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.util.log.Log;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.CaptchaRender;
import com.soft.idc.common.control.AuthInterceptor;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.util.JQPage;

public class MainPageController extends Controller {

	public void index() {
		/*int minutes = 10; 
		Date d = new Date(); 
		String modDate =  DateFormat.getDateInstance().format(d);
		String expDate = null; 
		expDate = DateFormat.getDateInstance().format((new Date(d.getTime() + minutes * 1000)));
		this.getResponse().setHeader("Last-Modified", modDate); 
		this.getResponse().setHeader("Expires", expDate); 
		this.getResponse().setHeader("Cache-Control", "public"); //   HTTP/1.1 
		this.getResponse().setHeader("Pragma", "Pragma"); //   HTTP/1.0 
		*/
		this.renderJsp("/mainpage/main.jsp");
	}

	// bi面板
	public void loadBI() {
		IdcUser u = this.getSessionAttr("SYS_USER");// 暂时修改
		
		String sql = "select id,pv,uv,num_user_ku,num_user_wifi,count_access,time_day,time_once,flight_online_num,flight_num,createtime"
				+ " from idc_bi_info where org_id = ? order by createtime desc limit 0,1";
		Record bi = Db.findFirst(sql,u.getOrgId());

		this.setAttr("bi", bi);
		this.renderJson();
	}

	// 航班面板
	public void loadFlightList() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		int pageNum = start / length + 1;
		// select
		// flight_id,flight_code,from_airport,to_airport,fly_time,land_time from
		// idc_flight_info;
		String sqlSelect = "select flight_id,flight_code,concat(from_airport,'-',to_airport) as line,fly_time,plan_land_time,land_time,"
				+ "(select name from idc_org where org_id = i.org_id) as org_name ";
		String sqlFrom = "from idc_flight_info i where status != ? ";
		IdcUser u = this.getSessionAttr("SYS_USER");


		if (u.getOrgId() == 1) {
			Page<Record> page = Db.paginate(pageNum, length, sqlSelect, sqlFrom, 9);

			JQPage jPage = new JQPage(page);
			this.renderJson(jPage);
		} else {
			sqlFrom += "and i.org_id = ?";
			Page<Record> page = Db.paginate(pageNum, length, sqlSelect, sqlFrom,9,u.getOrgId());
			JQPage jPage = new JQPage(page);
			this.renderJson(jPage);
		}
	}

	// 获取区域坐标
	public void loadAreaLable() {
		// 获取机场数据
		String sqlAirport = "select city,lng,lat,level from idc_airport_gps g";
		List<Record> airportList = Db.findByCache("MainPageCache", "air_port", sqlAirport);

		this.renderJson(airportList);
	}

	// 地图数据
	public void loadMapData() {

		// 获取航空公司lenged
		IdcUser u = this.getSessionAttr("SYS_USER");// 暂时修改
		String sql = "select org_id,name,color from idc_org where 1=1 and parent_id !=-1 and org_type = 0 ";
		List<Record> list = null;
		if (u.getOrgId() == 1) {
			list = Db.find(sql);
		} else {
			sql += "and org_id = ?";
			list = Db.find(sql, u.getOrgId());
		}

		String sql1 = "select a.flight_id,a.from_airport,a.to_airport from idc_flight_info a where a.org_id = ? and a.status != ? ";
		// 查找在线航班
		for (Record vo : list) {
			List<Record> lineList = Db.find(sql1, vo.get("org_id"), 9);
			vo.set("lineList", lineList);
		}

		this.renderJson(list);

	}

	// 读取health数据
	public void loadHealth() {

		// 获取航空公司lenged
		IdcUser u = this.getSessionAttr("SYS_USER");// 暂时修改
		String sql = "select net_health,net_use,device_use,device_health "
				+ "from idc_system_health where org_id = ? order by create_time desc limit 0,1";
		Record vo = Db.findFirstByCache("MainPageCache", "SYS_HEALTH_" + u.getOrgId(), sql, u.getOrgId());
		this.renderJson(vo);

	}

	// 读取流量
	public void loadFlow() {
		String createTime = this.getPara("create_time");
		IdcUser u = this.getSessionAttr("SYS_USER");// 暂时修改

		String sql = "select type,value,create_time from   idc_flow_count  where  type = ? and org_id = ? ";

		List<Record> three = null;
		List<Record> local = null;
		List<Record> vpn = null;
		List<Record> internet = null;
		List<Record> atg = null;

		if (createTime != null && !"".equals(createTime)) {
			sql += " and create_time > str_to_date(?,'%Y-%m-%d %H:%i:%s') order by create_time desc limit 0,5";
			three = Db.find(sql, new Object[] { "3G", u.getOrgId(), createTime });
			local = Db.find(sql, new Object[] { "LOCAL", u.getOrgId(), createTime });
			vpn = Db.find(sql, new Object[] { "KU", u.getOrgId(), createTime });
			internet = Db.find(sql, new Object[] { "internet", u.getOrgId(), createTime });
			atg = Db.find(sql, new Object[] { "ATG", u.getOrgId(), createTime });

		} else {
			sql += "order by create_time desc limit 0,5 ";
			three = Db.find(sql, new Object[] { "3G", u.getOrgId() });
			local = Db.find(sql, new Object[] { "LOCAL", u.getOrgId() });
			vpn = Db.find(sql, new Object[] { "KU", u.getOrgId() });
			internet = Db.find(sql, new Object[] { "internet", u.getOrgId() });
			atg = Db.find(sql, new Object[] { "ATG", u.getOrgId() });
		}

		this.setAttr("THREE", three);
		this.setAttr("LOCAL", local);
		this.setAttr("KU", vpn);
		this.setAttr("INTERNET", internet);
		this.setAttr("ATG", atg);

		this.renderJson();
	}

	public void loadWarn() {
		String createTime = this.getPara("create_time");
		IdcUser u = this.getSessionAttr("SYS_USER");// 暂时修改

		String sql = "select type,value,create_time from   idc_warn_count  where  type = ? and org_id = ? ";

		List<Record> local = null;
		List<Record> plane = null;
		List<Record> net = null;

		if (createTime != null && !"".equals(createTime)) {
			sql += " and create_time > str_to_date(?,'%Y-%m-%d %H:%i:%s') order by create_time desc limit 0,5";
			local = Db.find(sql, new Object[] { "LOCAL", u.getOrgId(), createTime });
			plane = Db.find(sql, new Object[] { "PLANE", u.getOrgId(), createTime });
			net = Db.find(sql, new Object[] { "NET", u.getOrgId(), createTime });

		} else {
			sql += "order by create_time desc limit 0,5 ";
			local = Db.find(sql, new Object[] { "LOCAL", u.getOrgId() });
			plane = Db.find(sql, new Object[] { "PLANE", u.getOrgId() });
			net = Db.find(sql, new Object[] { "NET", u.getOrgId() });
		}

		this.setAttr("LOCAL", local);
		this.setAttr("PLANE", plane);
		this.setAttr("NET", net);

		this.renderJson();
	}

	public void showDetail() {
		String flightId = this.getPara("flight_id");
		String sql = "select i.flight_code,from_airport,to_airport,fly_time,land_time,people,plan_land_time,status "
				+ "from idc_flight_info i where i.flight_id = ?";
		Record vo = Db.findFirst(sql, flightId);
		this.renderJson(vo);
	}

}
